﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Shake.Framework.View;
using Shake.Business.Infrastructures;
using Shake.Domain.Filters;
using Shake.Domain.Models;
using Shake.Framework.Context;
using Shake.Framework.Attributes;
using Shake.Business.Services;
using Shake.Domain.Emuns;

namespace Shake.Web.Controllers
{
    [Theme]
    public class UserController : Controller
    {
        public IUserService UserService { get; set; }
        public IPostInfoService _postInfoService { get; set; }

        public IDepartmentService DepartmentService { get; set; }
        public ICertificateService CertificateService { get; set; }
        //
        // GET: /Certificate/
        public ActionResult Index(UserFilter filter)
        {
            if (Request.IsAjaxRequest())
            {
                var result = UserService.GetUsers(filter);
                return this.ToJson(result);
            }
            else
            {
                var posts = _postInfoService.GetAll();
                ViewBag.Posts = posts;
                var treeDataSource = DepartmentService.AsTree();
                ViewBag.TreeDataSource = treeDataSource;

                var allDepartments = DepartmentService.GetAllDepartment(new DepartmentFilter());
                ViewBag.AllDepartments = allDepartments;
            }
            return this.Shake();
        }
        public ActionResult Chart()
        {
            var users = UserService.GetUsers(new UserFilter());
            var posts = _postInfoService.GetAll();
            var departs = DepartmentService.GetAllDepartment(new DepartmentFilter());
            var certifs = CertificateService.GetCertificate(new CertificateFilter()).Where(p => p.Type == CertificateType.护照);
            var passPortTypes = certifs.Select(p => p.Other).Distinct();
            List<PassPortInfo> passInfos = new List<PassPortInfo>();
            foreach (string pType in passPortTypes)
            {
                PassPortInfo paInfo = new PassPortInfo();
                paInfo.Name = pType;
                paInfo.Count = certifs.Where(p => p.Other == pType).Count();
                passInfos.Add(paInfo);
            }
            var hasPassPortUserIds = certifs.Select(p => p.UserId).Distinct();
            var hasNoPassPortUserCount = users.Count - users.Where(p => hasPassPortUserIds.Contains(p.Id)).Count();
            passInfos.Add(new PassPortInfo()
            {
                Name = "无护照",
                Count = hasNoPassPortUserCount
            });
            ViewBag.Posts = posts;
            ViewBag.Users = users;
            ViewBag.Departs = departs;
            ViewBag.PassInfos = passInfos;
            return this.Shake();
        }

        public ActionResult Save(List<User> users)
        {
            UserService.Save(users);
            return this.PostResult(new PostResult { Data = users });
        }

        public ActionResult Remove(List<User> users)
        {
            UserService.Remove(users);
            return this.PostResult(new PostResult());
        }

        public ActionResult Center()
        {
            var posts = _postInfoService.GetAll();
            ViewBag.Posts = posts;
            var treeDataSource = DepartmentService.AsTree();
            ViewBag.TreeDataSource = treeDataSource;

            var allDepartments = DepartmentService.GetAllDepartment(new DepartmentFilter());
            ViewBag.AllDepartments = allDepartments;

            var model = UserService.GetFirstUser();
            return this.Shake(model);
        }
    }
}